﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AddIn.EA.Architecture.ComponentLogic.ElementModel;

namespace AddIn.EA.Architecture.ComponentLogic.StandardProject.Builders.Service
{
    public class ElementBuilderResourceAccess : ElementBuilder
    {
        public override void BuildStructure(ComponentLogic.ElementModel.Package viewModel, AddIn.EA.Framework.RepositoryManager templateRepository, BusinessEntityStandardProject itemEntity)
        {
            BuildResourceAccessElement(viewModel, templateRepository, itemEntity);
        }
        public void BuildResourceAccessElement(ComponentLogic.ElementModel.Package viewModel, Framework.RepositoryManager templateRepository, BusinessEntityStandardProject itemEntity)
        {
            AddIn.EA.Framework.Package templatePackage = templateRepository.GetPackageByName("Servico").GetPackageByName("ResourceAccess");
            AddIn.EA.Framework.Element templateResourceAccessElement = templatePackage.GetElementByName("[entity_name]");

            //Setup the ResourceAccess package
            ComponentLogic.ElementModel.Package servicoPackage = GetViewModelPackage("Servico", viewModel, true);
            ComponentLogic.ElementModel.Package resourceAccessPackage = GetViewModelPackage("ResourceAccess", servicoPackage, true);

            String elementName = templateResourceAccessElement.Name.Replace("[entity_name]", itemEntity.Name);

            //Prevent existing item before include
            resourceAccessPackage.Elements.RemoveAll(it => it.Name.CompareTo(elementName) == 0);

            //Build ResourceAccess Element.
            Element resourceAccessElement = BuildEntityElementFromTemplate(itemEntity, templateResourceAccessElement, elementName);

            //Add the methods defined for this layer.
            BindElementMethods(resourceAccessElement, itemEntity, "BusinessEntity", "BusinessEntity");

            resourceAccessPackage.Elements.Add(resourceAccessElement);
        }

        public override void Validate()
        {
        }
    }
}